package com.caseProject.puremvc.mediator
{
	import com.caseProject.puremvc.mediator.view.MenuProjectView;
	import com.caseProject.puremvc.mediator.events.MenuProjectEvent;
	import com.caseProject.puremvc.model.DiagramNames;
	import com.caseProject.puremvc.notifications.NotificationNames;
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MenuProjectMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ProjectMenuMediator";
				
		public function MenuProjectMediator(viewComponent:MenuProjectView)
		{
			super(NAME, viewComponent);
			
			this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_ABRIR, onAbrir);
			this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_FECHAR, onFechar);
			this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_NOVO_PROJETO, onNovoProjeto);
		}
		
		public function get projectMenuView():MenuProjectView
		{
			return viewComponent as MenuProjectView;
		}
		
		override public function listNotificationInterests():Array
		{
			return [
						NotificationNames.OPEN_PROJECT_SUCESS,
						NotificationNames.CREATE_PROJECT_SUCESS
					]
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case NotificationNames.OPEN_PROJECT_SUCESS:
					this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_NOVO_DIAGRAMA, onNovoDiagrama);
					this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_SALVAR, onSalvar);
					break;
				
				case NotificationNames.CREATE_PROJECT_SUCESS:
					this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_NOVO_DIAGRAMA, onNovoDiagrama);
					this.projectMenuView.addEventListener(MenuProjectEvent.CLICK_SALVAR, onSalvar);
					break;				
			}
		}
		
		protected function onSalvar(event:Event):void
		{
			this.sendNotification(NotificationNames.PROJECT_MENU_SALVAR);
		}
		
		protected function onNovoProjeto(event:Event):void
		{
			this.sendNotification(NotificationNames.PROJECT_MENU_NOVO_PROJETO);			
		}
		
		protected function onNovoDiagrama(event:Event):void
		{
			this.sendNotification(NotificationNames.PROJECT_MENU_NOVO_DIAGRAMA);
		}
		
		protected function onFechar(event:Event):void
		{
			this.sendNotification(NotificationNames.PROJECT_MENU_FECHAR);
		}
		
		protected function onAbrir(event:Event):void
		{
			this.sendNotification(NotificationNames.PROJECT_MENU_ABRIR);
		}
	}
}